package development.game;

import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import state.Alert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.Key;
import state.TimerListener;
import state.Transformer;
import state.component.Event;
import state.component.EventListener;
import state.component.Menu;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.socket.DataReceiveListener;
import state.util.Color;
import state.util.ImageUtil;
import development.Constants;
import development.Global;
import development.bean.Game;
import development.bean.PhomInfo;
import development.bean.User;
import development.socket.SocketClientUtil;

public class FrmTala extends GameForm implements TimerListener, EventListener,
		DataReceiveListener {
	private static final int DELIVER_CARD_HEADER = 1;
	private static final int FIRED_CARD_HEADER = 2;
	private static final int BOC_BAI_HEADER = 3;
	private static final int AN_BAI_HEADER = 4;
	private static final int SEND_TOTAL_HEADER = 5;
	private static final int INVALID_ACTION = 6;
	private static final int VALID_ACTION = 7;
	private static final int HA_PHOM_DONE_HEADER = 8;
	private static final int HA_PHOM_HEADER = 9;
	private static final int HOST_CARD_HEADER = 10;
	private static final int TAKE_TURN = 11;
	private static final int REQUEST_CARD_HEADER = 12;
	private static final int TURN_ORDER = 13;
	private static final int RANDOM_CARD = 14;
	private static final int HA_PHOM_XONG_HEADER = 15; // Ket thuc ha
	private static final int HA_PHOM_SAI_HEADER = 16; // Phom ha khong hop le
	private static final int HA_PHOM_TIMEOUT_HEADER = 17;
	private static final int HOST_CARD_FAILED = 18;
	private static final int HOST_CARD_DONE = 19;
	private static final int HOST_CARD_SEND_HEADER = 20;
	private static final int OUT_TABLE = 21;

	private static final int CHAT_SEND = 22;
	private static final int GET_CARD_STATE = 23;
	private static final int FIRE_CARD_STATE = 24;
	private static final int WAIT_STATE = 25;
	private static final int HA_PHOM_STATE = 26;

	private static final int CARD_DISTANCE = 12;
	private static final int TURN_TIME_OUT = 30;
	private static final int[][] TIME_LOCATION = new int[][] {
			{ GameConstants.SCREEN_WIDTH / 2, 250 },
			{ GameConstants.SCREEN_WIDTH - 50, GameConstants.SCREEN_HEIGHT / 2 },
			{ GameConstants.SCREEN_WIDTH / 2, 50 },
			{ 50, GameConstants.SCREEN_HEIGHT / 2 } };
	private static final int[][] TIME_LOCATION_320x240 = new int[][] {
			{ GameConstants.SCREEN_WIDTH / 2, 170 },
			{ GameConstants.SCREEN_WIDTH - 50, GameConstants.SCREEN_HEIGHT / 2 },
			{ GameConstants.SCREEN_WIDTH / 2, 50 },
			{ 50, GameConstants.SCREEN_HEIGHT / 2 } };

	private Image baiUpImage;
	private Image pointerImage;
	private Image[] cardImges;
	private Image[] selectedCardImges;
	private Image[] avatarBgImages;
	private Image avatarDefautl;
	private Image numberBgImage;
	private Image[] ketQuaImage;
	private Image nhatImage;
	private Image uImage;

	private Image leftImage;
	private Image rightImage;
	private Image bodyFrameImage;

	private static Vector[] userCards;
	private static Vector[] firedCards;
	private static Vector[] gotCards;
	private static Vector[] phoms; // Phỏm
	private Vector selectedCardByUser;
	private Vector belongToPhoms;

	private int numberOfPlayer;
	private int state;
	private int messageIndex;
	private int movePointer;
	private int currentTurn;
	private int myIndex;
	private boolean isGotCard;
	private int lastFireCard;
	private ImageText text8;

	private boolean isShowingFinalResult;
	private String[] resultName;
	private String[] leftPoint;
	private boolean[] isShowUserCard;
	private int[] resultY;
	private int[] cardPoint;
	private String[] resultLabel;

	private int[] rank;

	private int currentTimeIndex;
	private int currentTimeCount;
	private int count;
	private int remainCard;
	private boolean isSomeOneU;
	private String userJidU;

	private StringBuffer currentText;
	private int caretIndex;
	private int caretLeft;
	private boolean goToNextChar;
	private int inputWidth;
	private int inputTranslationX;
	private long lastKeyTimestamp;
	private long maxKeyDelay;
	private int lastPressedKey;
	private int currentKeyStep;
	private int[] messageDelayCount;
	private String[] messageChats;
	private boolean isInChatState;
	private int timeShowResult;
	private boolean[] isTimeOut;
	private Menu menu;
	private String[] MENU;
	private boolean[] isHaphom;
	private int[] timeDisplayMom;

	private boolean isCardsChange;
	private boolean isGuiBai;
	public static Vector tableUsers = new Vector(); // Danh sách user trong bàn chơi hiện tại

	public void init(Hashtable inputParameters) {
	  manager.setBackGroundColor(0x440000);
	  
		belongToPhoms = new Vector();
		GameGlobal.setTimerDelay(100);

		tableUsers = Global.tableUsers;
		isTimeOut = new boolean[4];
		isHaphom = new boolean[4];
		timeDisplayMom = new int[4];
		for (int i = 0; i < 4; i++) {
			isTimeOut[i] = false;
			timeDisplayMom[i] = 0;
		}
		isCardsChange = false;
		isGuiBai = false;
		timeShowResult = 0;
		isGotCard = false;
		isSomeOneU = false;
		selectedCardByUser = new Vector();
		movePointer = 0;
		currentTurn = 0;
		numberOfPlayer = tableUsers.size();
		messageIndex = -1;
		currentTimeIndex = -1;
		currentTimeCount = -1;
		isShowingFinalResult = false;
		resultY = new int[] { 111, 129, 147, 165 };
		resultLabel = new String[] { "Nhất", "Nhì", "Ba", "Bét" };

		numberBgImage = ImageUtil.getImage("Nen_So.png");

		// Khởi tạo các ảnh quân bài
		cardImges = new Image[4];
		cardImges[3] = ImageUtil.getImage("Bai_Co_1.png"); // Cơ
		cardImges[2] = ImageUtil.getImage("Bai_Ro_1.png"); // Rô
		cardImges[1] = ImageUtil.getImage("Bai_Tep_1.png"); // Nhép
		cardImges[0] = ImageUtil.getImage("Bai_Bich_1.png"); // Bích

		ketQuaImage = new Image[4];
		ketQuaImage[0] = ImageUtil.getImage("mom.png");
		ketQuaImage[1] = ImageUtil.getImage("Nhi.png");
		ketQuaImage[2] = ImageUtil.getImage("Ba.png");
		ketQuaImage[3] = ImageUtil.getImage("Bet.png");
		nhatImage = ImageUtil.getImage("Nhat.png");
		uImage = ImageUtil.getImage("u.png");

		selectedCardImges = new Image[4];
		selectedCardImges[3] = ImageUtil.getImage("Bai_Co_2.png"); // Cơ
		selectedCardImges[2] = ImageUtil.getImage("Bai_Ro_2.png"); // Rô
		selectedCardImges[1] = ImageUtil.getImage("Bai_Tep_2.png"); // Nhép
		selectedCardImges[0] = ImageUtil.getImage("Bai_Bich_2.png"); // Bích

		avatarDefautl = ImageUtil.getImage("Avatar.png");
		avatarBgImages = new Image[4];
		avatarBgImages[0] = ImageUtil.getImage("Avatar.png");
		avatarBgImages[1] = ImageUtil.getImage("Avatar.png");
		avatarBgImages[2] = ImageUtil.getImage("Avatar.png");
		avatarBgImages[3] = ImageUtil.getImage("Avatar.png");

		Image popUpImage = ImageUtil.getImage("PopUp3.png");
		leftImage = ImageUtil.getSubImage(popUpImage, 0, 0, 14, 98, true);
		rightImage = ImageUtil.getSubImage(popUpImage, 21, 0, 12, 98, true);
		bodyFrameImage = ImageUtil.getSubImage(popUpImage, 17, 0, 1, 98, true);
		bodyFrameImage = ImageUtil.joinAndCreateImages(bodyFrameImage, 177, 98,
				true);
		popUpImage = null;

		baiUpImage = ImageUtil.getImage("Bai_MSau.png");
		pointerImage = ImageUtil.getImage("Muiten_x_p_t.png");

		text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
		state = WAIT_STATE;

		// Init menu
		MENU = new String[] { "Chơi tiếp", "Sắp xếp", "Thoát" };
		menu = new Menu(MENU, this, Constants.MENU_STYLE,
				GameConstants.SCREEN_WIDTH - 120,
				GameConstants.SCREEN_HEIGHT - 42);
		menu.setOpenChildMenuToRight(false);
		setTimerListener(this);
		DataPackage dataPackage = Game.createGamePackage(TURN_ORDER);
		Global.gameActionClient.write(dataPackage);
	}

	public void onRecieveData(DataPackage dataPackage) {
		int header = dataPackage.getHeader();
		switch (header) {
		case DELIVER_CARD_HEADER:
			// Nhận quân bài
			int index = dataPackage.nextInt();
			for (int i = 0; i < index; i++) {
				userCards[myIndex]
						.addElement(new Integer(dataPackage.nextInt()));
			}
			sapXepTangDanTheoCaNgang(userCards[myIndex]);
			// Add quân bài chưa biết vào cho các user khác
			for (int i = 0; i < userCards.length; i++) {
				if (userCards[i].size() == 0) {
					for (int j = 0; j < 9; j++) {
						userCards[i].addElement(new Integer(0));
					}
				}
			}
			isGotCard = true;
			break;
		case TURN_ORDER:
			Vector userListTmp = new Vector();
			String firstName = dataPackage.nextString();
			if (getUserByName(firstName) != null) {
				userListTmp.addElement(getUserByName(firstName));
			}
			int sizeUser = dataPackage.nextInt();
			for (int i = 0; i < sizeUser; i++) {
				String userName = dataPackage.nextString();
				if (getUserByName(userName) != null) {
					userListTmp.addElement(getUserByName(userName));
				}
			}
			tableUsers = userListTmp;
			myIndex = getUserIndex(Global.currentUser.getName());
			numberOfPlayer = tableUsers.size();
			initForGame();
			DataPackage movePackage = Game.createGamePackage(REQUEST_CARD_HEADER);
			Global.gameActionClient.write(movePackage);
			break;
		case TAKE_TURN:
			String userName = dataPackage.nextString();
			if (userName.equals(Global.currentUser.getName())) {
				if (userCards[myIndex].size() == 10) {
					state = FIRE_CARD_STATE;
				} else {
					state = GET_CARD_STATE;
				}
			}
			currentTimeIndex = getUserIndex(userName);
			currentTimeCount = TURN_TIME_OUT;
			count = 0;
			break;
		case FIRED_CARD_HEADER:
			int card = dataPackage.nextInt();
			String fromUser = dataPackage.nextString();
			int userIndex = getUserIndex(fromUser);
			System.out.println("Nguoi choi thu " + userIndex + " la "
					+ fromUser + "da danh quan bai " + card);
			firedCards[userIndex].addElement(new Integer(card));
			lastFireCard = card;
			currentTimeCount = -1;
			break;
		case BOC_BAI_HEADER:
			String userBoc = dataPackage.nextString();
			System.out.println("Nguoi choi boc bai la: " + userBoc
					+ "-Nguoi choi hien tai la" + Global.currentUser.getName());
			if (Global.currentUser.getName().equals(userBoc)) {
				int cardBoc = dataPackage.nextInt();
				userCards[myIndex].addElement(new Integer(cardBoc));
				state = FIRE_CARD_STATE;
			}
			remainCard--;
			break;
		case AN_BAI_HEADER:
			int cardAn = dataPackage.nextInt();
			String fromUserBiAn = dataPackage.nextString();
			String fromUserAn = dataPackage.nextString();
			int indexBiAn = getUserIndex(fromUserBiAn);
			int indexAn = getUserIndex(fromUserAn);
			System.out.println(">>>>>>>>>>>>>>>Nguoi bi an: " + fromUserBiAn);
			System.out.println(">>>>>>>>>>>>>>>Nguoi an: " + fromUserAn);
			gotCards[indexAn].addElement(firedCards[indexBiAn].elementAt(firedCards[indexBiAn].size() - 1));
			userCards[indexAn].removeElementAt(0);
			firedCards[indexBiAn].removeElementAt(firedCards[indexBiAn].size() - 1);
			sanBai(indexAn, indexBiAn);
			// Mới có ăn cây bình thường, chưa có play cho ăn cây chốt
			break;
		case HA_PHOM_SAI_HEADER:
			System.out.println(" vao day:  " + messageIndex);
			messageIndex = 6;
			break;
		case HA_PHOM_DONE_HEADER:
			movePointer = 0;
			int sizePhom = dataPackage.nextInt();
			Vector tmpPhom = new Vector();
			for (int i = 0; i < sizePhom; i++) {
				PhomInfo phomInfo = new PhomInfo();
				int sizePhom1 = dataPackage.nextInt();
				for (int j = 0; j < sizePhom1; j++) {
					int cardPhom = dataPackage.nextInt();
					phomInfo.addCard(cardPhom);
					belongToPhoms.addElement(new Integer(cardPhom));
					removeElementByValue(userCards[myIndex], cardPhom);
				}
				tmpPhom.addElement(phomInfo);
			}
			String userHa = dataPackage.nextString();

			int indexHa = getUserIndex(userHa);
			for (int i = 0; i < tmpPhom.size(); i++) {
				phoms[indexHa].addElement(tmpPhom.elementAt(i));
			}
			if (indexHa == myIndex) {
				isGuiBai = true;
			}
			isHaphom[indexHa] = true;
			currentTimeCount = TURN_TIME_OUT;
			selectedCardByUser.removeAllElements();
			state = FIRE_CARD_STATE;
			break;
		case HA_PHOM_HEADER:
			int sizeHaPhom = dataPackage.nextInt();
			System.out.println(">>>>>>>>>>>>>Do lon phom can ha: " + sizeHaPhom);
			movePointer = 0;
			Vector tmpPhomHaPhom = new Vector();
			for (int i = 0; i < sizeHaPhom; i++) {
				int sizePhom1 = dataPackage.nextInt();
				for (int j = 0; j < sizePhom1; j++) {
					int cardHaPhom = dataPackage.nextInt();
					tmpPhomHaPhom.addElement(new Integer(cardHaPhom));
				}
			}
			String userHaPhom = dataPackage.nextString();
			int indexHaPhom = getUserIndex(userHaPhom);
			if (indexHaPhom == myIndex) {
				selectedCardByUser.removeAllElements();
				for (int i = 0; i < tmpPhomHaPhom.size(); i++) {
					selectedCardByUser.addElement(tmpPhomHaPhom.elementAt(i));
				}
				currentTimeCount = TURN_TIME_OUT;
				currentTimeIndex = getUserIndex(userHaPhom);
				if (selectedCardByUser.size() > 0) {
					state = HA_PHOM_STATE;
					System.out.println(">>>>>>>>>>>Chuyen sang trang thai ha phom");
				} else {
					DataPackage movePackage1 = Game.createGamePackage(HA_PHOM_XONG_HEADER);
					movePackage1.putInt(0);
					Global.gameActionClient.write(movePackage1);
					System.out.println(">>>>>>>>>>>Ha phom xong");
				}
			}
			break;
		case SEND_TOTAL_HEADER:
			String userNameKetQua = dataPackage.nextString();
			int result1 = dataPackage.nextInt();
			int result2 = dataPackage.nextInt();
			int userIndexKetQua = getUserIndex(userNameKetQua);
			User user = (User) tableUsers.elementAt(userIndexKetQua);
			int point = result1;
			int rank = numberOfPlayer - 1;
			if (point == 0) { // Nếu người chơi ù
				rank = result2;
				isSomeOneU = true;
				userJidU = userNameKetQua;
			} else if (point == -1) {
				int check = dataPackage.nextInt();
				if (check == 0) { // Nếu là bét do ù
					for (int i = 1; i < resultName.length; i++) {
						if (resultName[i] == null || resultName[i].equals("")) {
							rank = i;
							break;
						}
					}
				} else { // nếu là móm
					rank = result2;
					int lengthCard = check;
					System.out.println(">>>>>>>>>>>>>>>>>>>Do lon la: " + lengthCard);
					userCards[userIndexKetQua].removeAllElements();
					for (int t = 0; t < lengthCard; t++) {
						int cardTotal = dataPackage.nextInt();
						System.out.println(">>>>>>>>>>Quan bai thu " + t + " la: " + cardTotal);
						userCards[userIndexKetQua].addElement(new Integer(cardTotal));
					}
				}
			} else {
				rank = result2;
				int lengthCard = dataPackage.nextInt();
				userCards[userIndexKetQua].removeAllElements();
				for (int t = 0; t < lengthCard; t++) {
					int cardTotal = dataPackage.nextInt();
					userCards[userIndexKetQua]
							.addElement(new Integer(cardTotal));
				}
			}

			if (rank < numberOfPlayer) {
				resultName[rank] = user.getName();
				cardPoint[rank] = point;
			}
			gotCards[userIndexKetQua].removeAllElements();
			isShowUserCard[userIndexKetQua] = true;
			isShowingFinalResult = true;
			currentTimeCount = -1;
			for (int i = 0; i < userCards[userIndexKetQua].size(); i++) {
				for (int j = 0; j < belongToPhoms.size(); j++) {
					if (userCards[userIndexKetQua].elementAt(i).equals(
							belongToPhoms.elementAt(j))) {
						userCards[userIndexKetQua].removeElementAt(i);
					}
				}
			}
			break;
		case HOST_CARD_FAILED:
			messageIndex = 7;
			break;
		case HOST_CARD_DONE:
			String userHost1 = dataPackage.nextString();
			int cardHost = dataPackage.nextInt();
			int indexPhom = dataPackage.nextInt();
			String userHost2 = dataPackage.nextString();
			int user1Index = getUserIndex(userHost1);
			int user2Index = getUserIndex(userHost2);

			// Thêm cây gửi vào phỏm của người chơi đã hạ
			for (int i = 0; i < phoms[user2Index].size(); i++) {
				PhomInfo phom = (PhomInfo) phoms[user2Index].elementAt(i);
				Vector temp1 = new Vector();
				for (int j = 0; j < phom.size(); j++) {
					temp1.addElement(new Integer(phom.getCard(j)));
				}
				if (isCaNgang(temp1, cardHost)) {
					phom.addCard(cardHost);
					break;
				} else if (isCaDoc(temp1)) {
					phom.addCard(cardHost);
					break;
				}
			}

			// Bỏ cây đã gửi ra khỏi bài của người chơi đã gửi bài
			if (userHost1.equals(Global.currentUser.getName())) {
				removeElementByValue(userCards[myIndex], cardHost);
				removeElementByValue(selectedCardByUser, cardHost);
			} else {
				userCards[user1Index].removeElementAt(0);
			}
			break;
		case HOST_CARD_HEADER:
			String userHost = dataPackage.nextString();
			int indexUser = getUserIndex(userHost);
			if (indexUser == myIndex) {
				isGuiBai = true;
				selectedCardByUser.removeAllElements();
				int sizeHot = dataPackage.nextInt();
				for (int t = 0; t < sizeHot; t++) {
					selectedCardByUser.addElement(new Integer(dataPackage
							.nextInt()));
				}
			}
			break;
		case RANDOM_CARD:
			int cardRandom = dataPackage.nextInt();
			lastFireCard = cardRandom;
			sendDanhBai(cardRandom);
			break;
		case VALID_ACTION:
			int headerAction = dataPackage.nextInt();
			switch (headerAction) {
			case AN_BAI_HEADER:
				System.out.println("VALID_ACTION================= ");
				System.out.println("AN_BAI_HEADER VALID_ACTION========");
				int latestFiredCard = dataPackage.nextInt();
				String userBiAn = dataPackage.nextString();
				String userAn = dataPackage.nextString();
				int before = getUserIndex(userBiAn);
				userCards[myIndex].addElement(new Integer(lastFireCard));
				gotCards[myIndex].addElement(new Integer(lastFireCard));
				firedCards[before]
						.removeElementAt(firedCards[before].size() - 1);
				selectedCardByUser.removeAllElements();
				state = FIRE_CARD_STATE;
				sanBai(myIndex, before);
				break;
			case FIRED_CARD_HEADER:
				firedCards[myIndex].addElement(new Integer(lastFireCard));
				removeElementByValue(userCards[myIndex], lastFireCard);
				if (movePointer > userCards[myIndex].size() - 1) {
					movePointer = userCards[myIndex].size() - 1;
				}
				selectedCardByUser.removeAllElements();
				currentTurn = currentTurn + 1;
				if (currentTurn > numberOfPlayer - 1) {
					currentTurn = 0;
				}
				state = WAIT_STATE;
				break;
			}
			break;
		case INVALID_ACTION:
			int headerInAction = dataPackage.nextInt();
			switch (headerInAction) {
			case AN_BAI_HEADER:
				messageIndex = 0;
				break;
			case FIRED_CARD_HEADER:
				messageIndex = 1;
				break;
			}
			break;
		case OUT_TABLE:
			String userOut = dataPackage.nextString();
			if (!userOut.equals(Global.currentUser.getName())) {
				for (int i = 0; i < tableUsers.size(); i++) {
					User userRemove = (User) tableUsers.elementAt(i);
					if (userRemove.getName().equals(userOut)) {
						//tableUsers.removeElementAt(i);						
						isTimeOut[i] = true;
					}
				}
				System.out.println(">>>>>>>>.Con tong cong " + tableUsers.size() + " nguoi");
			}
//			numberOfPlayer--;
//			currentTimeIndex = getUserIndex(userOut);
//			if(myIndex != getUserIndex(userOut)){
//				currentTimeIndex++;
//				if(currentTimeIndex >= numberOfPlayer)
//					currentTimeIndex = 0;
//			}
			break;
		}
	}

	private void initForGame() {
		switch (numberOfPlayer) {
		case 2:
			remainCard = 7;
			break;
		case 3:
			remainCard = 11;
			break;
		case 4:
			remainCard = 15;
			break;
		}

		userCards = new Vector[numberOfPlayer];
		firedCards = new Vector[numberOfPlayer];
		gotCards = new Vector[numberOfPlayer];
		phoms = new Vector[numberOfPlayer];
		resultName = new String[numberOfPlayer];
		leftPoint = new String[numberOfPlayer];
		isShowUserCard = new boolean[numberOfPlayer];
		cardPoint = new int[numberOfPlayer];
		rank = new int[numberOfPlayer];
		messageDelayCount = new int[numberOfPlayer];
		messageChats = new String[numberOfPlayer];
		for (int i = 0; i < userCards.length; i++) {
			userCards[i] = new Vector();
			firedCards[i] = new Vector();
			gotCards[i] = new Vector();
			phoms[i] = new Vector();
			resultName[i] = "";
			leftPoint[i] = "";
			cardPoint[i] = -1;
			rank[i] = -1;
			messageDelayCount[i] = -1;
			isShowUserCard[i] = false;
			messageChats[i] = "";
		}

		isInChatState = false;
		inputWidth = 90;
		resetText();
	}

	private User getUserByName(String userName) {
		for (int i = 0; i < tableUsers.size(); i++) {
			if (userName.equals(((User) tableUsers.elementAt(i))
					.getName())) {
				return (User) tableUsers.elementAt(i);
			}
		}
		return null;
	}

	private int getUserIndex(String userName) {
		for (int i = 0; i < tableUsers.size(); i++) {
			if (userName.equals(((User) tableUsers.elementAt(i))
					.getName())) {
				return i;
			}
		}
		return -1;
	}

	private void resetText() {
		if (messageDelayCount != null) {
			messageDelayCount[myIndex] = 0;
			messageChats[myIndex] = new String();
		}

		currentText = new StringBuffer();
		lastPressedKey = Integer.MIN_VALUE;
		currentKeyStep = 0;

		inputTranslationX = 0;

		lastKeyTimestamp = 0;
		maxKeyDelay = 500L;

		caretIndex = 0;
		caretLeft = 0;

		goToNextChar = true;
	}

	public void checkTimestamps() {
		long currentTime = System.currentTimeMillis();

		if (!goToNextChar && lastKeyTimestamp + maxKeyDelay < currentTime) {
			goToNextChar = true;
		}
	}

	private char[] getChars(int key) {
		switch (key) {
		case Key.K_1:
			return Constants.KEY_NUM1_CHARS;
		case Key.K_2:
			return Constants.KEY_NUM2_CHARS;
		case Key.K_3:
			return Constants.KEY_NUM3_CHARS;
		case Key.K_4:
			return Constants.KEY_NUM4_CHARS;
		case Key.K_5:
			return Constants.KEY_NUM5_CHARS;
		case Key.K_6:
			return Constants.KEY_NUM6_CHARS;
		case Key.K_7:
			return Constants.KEY_NUM7_CHARS;
		case Key.K_8:
			return Constants.KEY_NUM8_CHARS;
		case Key.K_9:
			return Constants.KEY_NUM9_CHARS;
		case Key.K_0:
			return Constants.KEY_NUM0_CHARS;
		case Key.STAR:
			return Constants.KEY_STAR_CHARS;
		}
		return null;
	}

	private void updateCaretPosition() {
		caretLeft = text8.substringWidth(messageChats[myIndex], 0, caretIndex);
		if (caretLeft + inputTranslationX < 0) {
			inputTranslationX = -caretLeft;
		} else if (caretLeft + inputTranslationX > inputWidth) {
			inputTranslationX = inputWidth - caretLeft;
		}
	}

	private void clearChar() {
		if (currentText.length() > 0 && caretIndex > 0) {
			caretIndex--;
			currentText.deleteCharAt(caretIndex);
			messageChats[myIndex] = currentText.toString();
		}
	}

	/**
	 * Sau khi 1 cây bài bị ăn thì cần kiểm tra và san những lá bài đã đánh cho
	 * đều
	 */
	private void sanBai(int index, int beforeIndex) {
		int tmp = index;
		Object changeCard;
		for (int i = 0; i < numberOfPlayer; i++) {
			if (firedCards[beforeIndex].size() < firedCards[tmp].size()) {
				changeCard = firedCards[tmp].lastElement();
				firedCards[beforeIndex].addElement(changeCard);
				firedCards[tmp].removeElement(changeCard);
				break;
			} else {
				tmp = behindUser(tmp);
			}

		}
	}

	private int behindUser(int index) {
		int behind = 0;
		if (index == numberOfPlayer - 1) {
			behind = 0;
		} else {
			behind = (index + 1) % numberOfPlayer;
		}
		while (isTimeOut[behind]) {
			behind = (behind + 1) % numberOfPlayer;
		}
		return behind;
	}

	private int getRealIndex(int position) {
		position = myIndex + position;
		position = position % numberOfPlayer;
		return position;
	}

	private int getDrawIndex(int index) {
		index = (index - myIndex + numberOfPlayer) % numberOfPlayer;
		return index;
	}

	public void writeKeyPressed(int key) {
		char[] chars = getChars(key);
		if (goToNextChar || (key != lastPressedKey)) {
			goToNextChar = true;
			lastPressedKey = key;
			currentKeyStep = 0;
		} else {
			currentKeyStep++;
		}

		if (chars != null) {
			if (currentKeyStep >= chars.length) {
				currentKeyStep -= chars.length;
			}
			if (goToNextChar) {
				if (currentText.length() < 25) {
					currentText.insert(caretIndex, chars[currentKeyStep]);
					caretIndex++;
				}
			} else {
				currentText.setCharAt(caretIndex - 1, chars[currentKeyStep]);
			}
			messageChats[myIndex] = currentText.toString();
			updateCaretPosition();
			lastKeyTimestamp = System.currentTimeMillis();
			goToNextChar = false;
		}
	}

	public void writeKeyPressed320x240(int key) {
		if (goToNextChar || (key != lastPressedKey)) {
			goToNextChar = true;
			lastPressedKey = key;
			currentKeyStep = 0;
		} else {
			currentKeyStep++;
		}

		char charKey = (char) key;
		if (goToNextChar) {
			if (currentText.length() < 25) {
				currentText.insert(caretIndex, charKey);
				caretIndex++;
			}
		} else {
			currentText.setCharAt(caretIndex - 1, charKey);
		}
		messageChats[myIndex] = currentText.toString();
		updateCaretPosition();
		lastKeyTimestamp = System.currentTimeMillis();
		goToNextChar = false;
	}

	public void keyReleased(int keyCode) {
		keyCode = Key.getGameKey(keyCode);

		if (keyCode == Key.SOFT_LEFT) {
			if (!isInChatState) {

				// Global.isInitSuccess = false;
				switch (state) {
				case GET_CARD_STATE:
					// AN_BAI_HEADER:2:0405
					if (selectedCardByUser.size() > 0) {
						DataPackage movePackage = Game
								.createGamePackage(AN_BAI_HEADER);
						movePackage.putInt(selectedCardByUser.size());
						for (int i = 0; i < selectedCardByUser.size(); i++) {
							movePackage.putInt(getInt(selectedCardByUser, i));
						}
						Global.gameActionClient.write(movePackage);
					} else {
						messageIndex = 4; // Thông báo là cần phải nhấc bài lên
						// để ăn quân
					}
					break;
				case FIRE_CARD_STATE:
					if (isGuiBai) {
						if (selectedCardByUser.size() > 0) {
							DataPackage movePackage = Game
									.createGamePackage(HOST_CARD_SEND_HEADER);
							movePackage.putInt(selectedCardByUser.size());
							for (int i = 0; i < selectedCardByUser.size(); i++) {
								movePackage
										.putInt(getInt(selectedCardByUser, i));
							}
							Global.gameActionClient.write(movePackage);
						}

					}
					break;
				case WAIT_STATE:
					break;
				case HA_PHOM_STATE:
					break;
				}
				// Global.isInitSuccess = true;
			} else {
				isInChatState = false;
			}
		}

		if (isShowingFinalResult) {
			if (keyCode == Key.FIRE) {
				Global.frmTable.isReturnFromGame = true;
				GameGlobal.nextState(Global.frmTable, null,
						Transformer.TRANSFORM_WITH_LOADING_FORM);
			}

			if (keyCode == Key.SOFT_RIGHT) {
				SocketClientUtil.leaveTableRequest();
				GameGlobal.nextState(Global.frmListTable, null,
						Transformer.TRANSFORM_WITH_LOADING_FORM);
			}
			return;
		}

		if ((keyCode == Key.SOFT_RIGHT) && isInChatState) {
			if ((currentText != null) && (currentText.length() > 0)) {
				clearChar();
			} else {
				isInChatState = false;
			}
		}

		// Kiểm tra để hiển thị menu
		if (keyCode == Key.SOFT_RIGHT && !isInChatState) {
			isInChatState = false;
			if (menu.isShowing()) {
				menu.hide();
			} else {
				menu.show();
			}
			return;
		}
		// Nếu menu đang hiển thị thì thoát
		if (menu.isShowing()) {
			return;
		}

		if ((keyCode >= Key.K_0) && (keyCode <= Key.K_9)
				&& GameConstants.IS_240x320_SCREEN) {
			if (!isInChatState) {
				resetText();
			}
			isInChatState = true;
			writeKeyPressed(keyCode);
			return;
		}

		boolean isCommandKey = Key.isCommandKey;
		if (!GameConstants.IS_240x320_SCREEN && !isCommandKey) {
			if (!isInChatState) {
				resetText();
			}
			isInChatState = true;
			writeKeyPressed320x240(keyCode);
			return;
		}

		switch (keyCode) {
		case Key.LEFT:
		case Key.K_4:
			// Global.isInitSuccess = false;
			int move = movePointer;
			movePointer--;
			if (movePointer < 0) {
				movePointer = userCards[myIndex].size() - 1;
			}
			messageIndex = -1;

			if (isCardsChange) {
				Object baiTemp = userCards[myIndex].elementAt(move);
				userCards[myIndex].setElementAt(userCards[myIndex]
						.elementAt(movePointer), move);
				userCards[myIndex].setElementAt(baiTemp, movePointer);
			}
			// Global.isInitSuccess = true;
			break;
		case Key.RIGHT:
		case Key.K_6:
			// Global.isInitSuccess = false;
			int move1 = movePointer;
			movePointer++;
			if (movePointer > userCards[myIndex].size() - 1) {
				movePointer = 0;
			}
			messageIndex = -1;
			if (isCardsChange) {
				Object baiTemp = userCards[myIndex].elementAt(move1);
				userCards[myIndex].setElementAt(userCards[myIndex]
						.elementAt(movePointer), move1);
				userCards[myIndex].setElementAt(baiTemp, movePointer);
			}
			// Global.isInitSuccess = true;
			break;
		case Key.FIRE:
		case Key.K_5:
			// Global.isInitSuccess = false;
			if (isInChatState) {
				updateCaretPosition();
				DataPackage movePackage = Game.createGamePackage(CHAT_SEND);
				movePackage.putString(messageChats[myIndex]);
				Global.gameActionClient.write(movePackage);
				isInChatState = false;
				// Global.isInitSuccess = true;
				return;
			}

			messageIndex = -1;
			if (state == HA_PHOM_STATE) {
				DataPackage movePackage = Game
						.createGamePackage(HA_PHOM_XONG_HEADER);
				movePackage.putInt(selectedCardByUser.size());
				for (int i = 0; i < selectedCardByUser.size(); i++) {
					movePackage.putInt(getInt(selectedCardByUser, i));
				}
				Global.gameActionClient.write(movePackage);
			}

			switch (state) {
			case GET_CARD_STATE:
				DataPackage movePackage = Game.createGamePackage(BOC_BAI_HEADER);
				Global.gameActionClient.write(movePackage);
				break;
			case FIRE_CARD_STATE:
				int cardValue = getInt(userCards[myIndex], movePointer);
				lastFireCard = cardValue;
				int index = getIndexByValue(selectedCardByUser, cardValue);
				if (index > -1) {
					sendDanhBai(cardValue);
				} else {
					messageIndex = 5; // Thông báo là cần phải nhấc bài lên để
					// đánh
				}
				break;
			}
			// Global.isInitSuccess = true;
			break;
		case Key.UP:
		case Key.K_2:
			// Global.isInitSuccess = false;
			messageIndex = -1;

			switch (state) {
			case GET_CARD_STATE:
			case FIRE_CARD_STATE:
			case WAIT_STATE:
			case HA_PHOM_STATE:
				if (!isCardsChange) {
					int card = Integer
							.parseInt(String.valueOf(userCards[myIndex]
									.elementAt(movePointer)));
					if (getIndexByValue(gotCards[myIndex], card) == -1
							|| state == HA_PHOM_STATE) {
						selectedCardByUser.addElement(userCards[myIndex]
								.elementAt(movePointer));
					}
				}
				break;
			}
			isCardsChange = false;
			// Global.isInitSuccess = true;
			break;
		case Key.DOWN:
		case Key.K_8:
			// Global.isInitSuccess = false;
			messageIndex = -1;
			if (getIndexByValue(selectedCardByUser, getInt(userCards[myIndex],
					movePointer)) <= -1) {
				isCardsChange = true;
			}
			switch (state) {
			case GET_CARD_STATE:
			case FIRE_CARD_STATE:
			case WAIT_STATE:
			case HA_PHOM_STATE:
				removeElementByValue(selectedCardByUser, getInt(
						userCards[myIndex], movePointer));
				break;
			}
			// Global.isInitSuccess = true;
			break;
		}
	}

	private int getInt(Vector vector, int index) {
		if (index > vector.size() - 1) {
			return 0;
		}
		return Integer.parseInt(String.valueOf(vector.elementAt(index)));
	}

	private int getIndexByValue(Vector vector, int value) {
		for (int i = 0; i < vector.size(); i++) {
			if (getInt(vector, i) == value) {
				return i;
			}
		}
		return -1;
	}

	private void removeElementByValue(Vector vector, int value) {
		for (int i = 0; i < vector.size(); i++) {
			if (getInt(vector, i) == value) {
				vector.removeElementAt(i);
				return;
			}
		}
	}

	private void sendDanhBai(int firedCard) {
		DataPackage movePackage = Game.createGamePackage(FIRED_CARD_HEADER);
		movePackage.putInt(firedCard);
		Global.gameActionClient.write(movePackage);
	}

	public void doTask() {
		// if (Display.getDisplay(GameGlobal.getMidlet()).getCurrent() ==
		// systemCanvas) {
		// checkTimestamps();
		// }
		count = (count + 1) % 1000;
		if (count % 10 == 0) {
			if (currentTimeCount > -1) {
				currentTimeCount--;
				if ((currentTimeCount == 3) && (currentTimeIndex == myIndex)) {
					if (state == GET_CARD_STATE) {
						DataPackage movePackage = Game
								.createGamePackage(BOC_BAI_HEADER);
						Global.gameActionClient.write(movePackage);
					}
				}
				if ((currentTimeCount == -1) && (currentTimeIndex == myIndex)) {
					if (state == HA_PHOM_STATE) {
						DataPackage movePackage = Game
								.createGamePackage(HA_PHOM_TIMEOUT_HEADER);
						Global.gameActionClient.write(movePackage);
					} else {
						DataPackage movePackage = Game
								.createGamePackage(RANDOM_CARD);
						Global.gameActionClient.write(movePackage);
					}
				}
			}
		}

		if (messageDelayCount != null) {
			for (int i = 0; i < messageDelayCount.length; i++) {
				if (messageDelayCount[i] > -1) {
					messageDelayCount[i]++;
					if (messageDelayCount[i] > 60) {
						messageChats[i] = "";
						messageDelayCount[i] = -1;
					}
				}
			}
		}
		if (isShowingFinalResult) {
			timeShowResult++;
		}
		if (timeShowResult == 150) {
			GameGlobal.nextState(Global.frmTable, null,
					Transformer.TRANSFORM_WITH_LOADING_FORM);
			return;
		}
		for (int i = 0; i < 4; i++) {
			if (isHaphom[i] && timeDisplayMom[i] <= 100) {
				timeDisplayMom[i]++;
			}
		}
	}

	public void draw(Graphics g) {
		try {
			if (GameConstants.IS_240x320_SCREEN) {
				if (isGotCard) {
					drawFiredCard(g);
					drawPhom(g);
					drawUserCards(g, TIME_LOCATION);
					drawUserInfo(g);
					drawCommand(g);
					drawMessage(g);
					drawChat(g);
				}
				if (isShowingFinalResult) {
					drawFinalResult(g, TIME_LOCATION);
				}
			} else {
				if (isGotCard) {
					drawFiredCard320x240(g);
					drawPhom320x240(g);
					drawUserCards(g, TIME_LOCATION_320x240);
					drawUserInfo320x240(g);
					drawCommand(g);
					drawMessage(g);
					drawChat320240(g);
				}
				if (isShowingFinalResult) {
					drawFinalResult(g, TIME_LOCATION_320x240);
				}
			}

			g.setClip(0, 0, GameConstants.SCREEN_WIDTH,
					GameConstants.SCREEN_HEIGHT);
			menu.draw(g);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void drawFinalResult(Graphics g, int[][] timeLocation) {
		// Vẽ ảnh hiệu ứng thứ hạng
		for (int i = 0; i < numberOfPlayer; i++) {
			int index = getDrawIndex(i);
			int drawIndex = index;
			if (numberOfPlayer == 2) {
				if (index == 1) {
					drawIndex = 2;
				}
			} else if (numberOfPlayer == 3) {
				if (index > 0) {
					drawIndex++;
				}
			}

			if (isSomeOneU) { // nếu người đứng nhất ù
				int uUserIndex = getUserIndex(userJidU);
				if (uUserIndex == i) {
					// g.drawImage(ketQuaImage[0], timeLocation[drawIndex][0],
					// timeLocation[drawIndex][1], GameConstants.CENTER_ANCHOR);
					g.drawImage(uImage, timeLocation[drawIndex][0],
							timeLocation[drawIndex][1],
							GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(ketQuaImage[3], timeLocation[drawIndex][0],
							timeLocation[drawIndex][1],
							GameConstants.CENTER_ANCHOR);
				}
			} else {
				if (rank[i] >= 0) {
					g.drawImage(ketQuaImage[rank[i]],
							timeLocation[drawIndex][0],
							timeLocation[drawIndex][1],
							GameConstants.CENTER_ANCHOR);
					if (rank[i] == 0) {
						g.drawImage(nhatImage, timeLocation[drawIndex][0],
								timeLocation[drawIndex][1],
								GameConstants.CENTER_ANCHOR);
					}
				}
			}
		}

		if (GameConstants.IS_240x320_SCREEN) {
			// Vẽ bảng kết quả
			g.drawImage(leftImage, 18, 82, GameConstants.TOP_LEFT_ANCHOR);
			g.drawImage(bodyFrameImage, 32, 82, GameConstants.TOP_LEFT_ANCHOR);
			g.drawImage(rightImage, 209, 82, GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "KẾT QUẢ", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2, 84,
					GameConstants.TOP_HCENTER_ANCHOR);
			g.setColor(Color.WHITE_CODE);
			g.drawLine(35, 101, 205, 101);

			for (int i = 0; i < numberOfPlayer; i++) {
				text8.drawString(g, resultLabel[i], Color.WHITE_CODE, 33,
						resultY[i], GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, resultName[i], Color.WHITE_CODE, 60,
						resultY[i], GameConstants.TOP_LEFT_ANCHOR);
				if (cardPoint[i] > 0) {
					text8.drawString(g, cardPoint[i] + "đ", Color.WHITE_CODE,
							115, resultY[i], GameConstants.TOP_LEFT_ANCHOR);
				} else if (cardPoint[i] == 0) {
					text8.drawString(g, "ù", Color.WHITE_CODE, 110, resultY[i],
							GameConstants.TOP_LEFT_ANCHOR);
				} else if (cardPoint[i] == -1) {
					if (cardPoint[0] == 0) { // Nếu có người ù
						text8.drawString(g, "bét", Color.WHITE_CODE, 110,
								resultY[i], GameConstants.TOP_LEFT_ANCHOR);
					} else {
						text8.drawString(g, "móm", Color.WHITE_CODE, 110,
								resultY[i], GameConstants.TOP_LEFT_ANCHOR);
					}
				}
				text8.drawString(g, leftPoint[i] + " điểm", Color.WHITE_CODE,
						155, resultY[i], GameConstants.TOP_LEFT_ANCHOR);
			}
		} else {
			// Vẽ bảng kết quả
			g.drawImage(leftImage, 58, 82, GameConstants.TOP_LEFT_ANCHOR);
			g.drawImage(bodyFrameImage, 72, 82, GameConstants.TOP_LEFT_ANCHOR);
			g.drawImage(rightImage, 249, 82, GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "KẾT QUẢ", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2, 84,
					GameConstants.TOP_HCENTER_ANCHOR);
			g.setColor(Color.WHITE_CODE);
			g.drawLine(75, 101, 240, 101);

			for (int i = 0; i < numberOfPlayer; i++) {
				text8.drawString(g, resultLabel[i], Color.WHITE_CODE, 73,
						resultY[i], GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, resultName[i], Color.WHITE_CODE, 100,
						resultY[i], GameConstants.TOP_LEFT_ANCHOR);
				if (cardPoint[i] > 0) {
					text8.drawString(g, cardPoint[i] + "đ", Color.WHITE_CODE,
							155, resultY[i], GameConstants.TOP_LEFT_ANCHOR);
				} else if (cardPoint[i] == 0) {
					text8.drawString(g, "ù", Color.WHITE_CODE, 155, resultY[i],
							GameConstants.TOP_LEFT_ANCHOR);
				} else if (cardPoint[i] == -1) {
					if (cardPoint[0] == 0) { // Nếu có người ù
						text8.drawString(g, "bét", Color.WHITE_CODE, 155,
								resultY[i], GameConstants.TOP_LEFT_ANCHOR);
					} else {
						text8.drawString(g, "móm", Color.WHITE_CODE, 155,
								resultY[i], GameConstants.TOP_LEFT_ANCHOR);
					}
				}
				text8.drawString(g, leftPoint[i] + " điểm", Color.WHITE_CODE,
						200, resultY[i], GameConstants.TOP_LEFT_ANCHOR);
			}
		}
	}

	private void drawUserInfo320x240(Graphics g) {
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1);
			User user = (User) tableUsers.elementAt(index);
			Image avatar = (Image) Global.getAvatar(user.getName());
			g.drawImage(avatarBgImages[2], 231, 0,
					GameConstants.TOP_LEFT_ANCHOR);
			if (avatar != null) {
				g.drawImage(avatar, 231, 0, GameConstants.TOP_LEFT_ANCHOR);
			} else {
				g.drawImage(avatarDefautl, 231, 0,
						GameConstants.TOP_LEFT_ANCHOR);
			}
			text8.drawString(g, user.getName(), Color.WHITE_CODE, 269, 8,
					GameConstants.TOP_LEFT_ANCHOR);
		} else if (numberOfPlayer == 3) {
			int index = getRealIndex(1);
			User user = (User) tableUsers.elementAt(index);
			Image avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[1], 231, 0,
						GameConstants.TOP_LEFT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 231, 0, GameConstants.TOP_LEFT_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 231, 0,
							GameConstants.TOP_LEFT_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 269, 8,
						GameConstants.TOP_LEFT_ANCHOR);
			}
			index = getRealIndex(2);
			user = (User) tableUsers.elementAt(index);
			avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[2], 0, 0,
						GameConstants.TOP_LEFT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 0, 0,
							GameConstants.TOP_LEFT_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 3, 33,
						GameConstants.TOP_LEFT_ANCHOR);
			}

		} else if (numberOfPlayer == 4) {
			int index = getRealIndex(1);
			User user = (User) tableUsers.elementAt(index);
			Image avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[1], GameConstants.SCREEN_WIDTH, 181,
						GameConstants.TOP_RIGHT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, GameConstants.SCREEN_WIDTH, 181,
							GameConstants.TOP_RIGHT_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, GameConstants.SCREEN_WIDTH, 181,
							GameConstants.TOP_RIGHT_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE,
						GameConstants.SCREEN_WIDTH, 167,
						GameConstants.TOP_RIGHT_ANCHOR);
			}
			index = getRealIndex(2);
			user = (User) tableUsers.elementAt(index);
			avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[2], 231, 0,
						GameConstants.TOP_LEFT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 231, 0, GameConstants.TOP_LEFT_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 231, 0,
							GameConstants.TOP_LEFT_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 269, 8,
						GameConstants.TOP_LEFT_ANCHOR);
			}
			index = getRealIndex(3);
			user = (User) tableUsers.elementAt(index);
			avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[3], 0, 0,
						GameConstants.TOP_LEFT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 0, 0,
							GameConstants.TOP_LEFT_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 3, 33,
						GameConstants.TOP_LEFT_ANCHOR);
			}
		}

		// Vẽ thông tin người chơi hiện tại
		g.drawImage(avatarBgImages[0], 70, 201, GameConstants.CENTER_ANCHOR);
		if (Global.userAvatar != null) {
			g
					.drawImage(Global.userAvatar, 70, 201,
							GameConstants.CENTER_ANCHOR);
		} else {
			g.drawImage(avatarDefautl, 70, 201, GameConstants.CENTER_ANCHOR);
		}

		text8.drawString(g, Global.currentUser.getName(), Color.WHITE_CODE, 16,
				190, GameConstants.TOP_LEFT_ANCHOR);
		text8.drawString(g, String.valueOf(Global.currentUser.getMoney()),
				Color.YELLOW_CODE, 16, 201, GameConstants.TOP_LEFT_ANCHOR);
	}

	private void drawUserInfo(Graphics g) {
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1);
			User user = (User) tableUsers.elementAt(index);
			Image avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[2], 236, 5,
						GameConstants.TOP_RIGHT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 221, 18, GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 221, 18,
							GameConstants.CENTER_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 236, 39,
						GameConstants.TOP_RIGHT_ANCHOR);
			}
		} else if (numberOfPlayer == 3) {
			int index = getRealIndex(1);
			User user = (User) tableUsers.elementAt(index);
			Image avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[1], 236, 5,
						GameConstants.TOP_RIGHT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 221, 18, GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 221, 18,
							GameConstants.CENTER_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 236, 39,
						GameConstants.TOP_RIGHT_ANCHOR);
			}
			index = getRealIndex(2);
			user = (User) tableUsers.elementAt(index);
			avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[2], 6, 40,
						GameConstants.TOP_LEFT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 21, 53, GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 21, 53,
							GameConstants.CENTER_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 6, 26,
						GameConstants.TOP_LEFT_ANCHOR);
			}

		} else if (numberOfPlayer == 4) {
			int index = getRealIndex(1);
			User user = (User) tableUsers.elementAt(index);
			Image avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[1], 236, 261,
						GameConstants.BOTTOM_RIGHT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 221, 244, GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 221, 244,
							GameConstants.CENTER_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 236, 274,
						GameConstants.TOP_RIGHT_ANCHOR);
			}

			index = getRealIndex(2);
			user = (User) tableUsers.elementAt(index);
			avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[2], 236, 5,
						GameConstants.TOP_RIGHT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 221, 18, GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 221, 18,
							GameConstants.CENTER_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 236, 39,
						GameConstants.TOP_RIGHT_ANCHOR);
			}

			index = getRealIndex(3);
			user = (User) tableUsers.elementAt(index);
			avatar = (Image) Global.getAvatar(user.getName());
			if (!isTimeOut[index]) {
				g.drawImage(avatarBgImages[3], 6, 40,
						GameConstants.TOP_LEFT_ANCHOR);
				if (avatar != null) {
					g.drawImage(avatar, 21, 53, GameConstants.CENTER_ANCHOR);
				} else {
					g.drawImage(avatarDefautl, 21, 53,
							GameConstants.CENTER_ANCHOR);
				}
				text8.drawString(g, user.getName(), Color.WHITE_CODE, 6, 16,
						GameConstants.TOP_LEFT_ANCHOR);
			}
		}

		// Vẽ thông tin người chơi hiện tại
		g.drawImage(avatarBgImages[0], 5, 258, GameConstants.TOP_LEFT_ANCHOR);
		if (Global.userAvatar != null) {
			g.drawImage(Global.userAvatar, 5, 258,
					GameConstants.TOP_LEFT_ANCHOR);
		}
		text8.drawString(g, Global.currentUser.getName(), Color.WHITE_CODE, 5,
				234, GameConstants.TOP_LEFT_ANCHOR);
		text8.drawString(g, String.valueOf(Global.currentUser.getMoney()),
				Color.YELLOW_CODE, 5, 245, GameConstants.TOP_LEFT_ANCHOR);
	}

	private void drawChat320240(Graphics g) {
		if (messageChats == null) {
			return;
		}

		for (int i = 0; i < messageChats.length; i++) {
			if (messageChats[i].equals("")) {
				continue;
			}

			int index = getDrawIndex(i);
			if (numberOfPlayer == 2) {
				if (index == 1) {
					index = 2;
				}
			} else if (numberOfPlayer == 3) {
				if (index > 0) {
					index++;
				}
			}

			switch (index) {
			case 1:
				// g.drawImage(groundChatImage, 215, 100,
				// GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, messageChats[i], Color.WHITE_CODE, 222,
						104, GameConstants.TOP_LEFT_ANCHOR);
				break;
			case 2:
				// g.drawImage(groundChatImage, 105, 10,
				// GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, messageChats[i], Color.WHITE_CODE, 110, 12,
						GameConstants.TOP_LEFT_ANCHOR);
				break;
			case 3:
				// g.drawImage(groundChatImage, 12, 100,
				// GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, messageChats[i], Color.WHITE_CODE, 17, 103,
						GameConstants.TOP_LEFT_ANCHOR);
				break;
			}
		}

		if (isInChatState) {
			// g.drawImage(groundChatImage, 100, 193,
			// GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, messageChats[myIndex], Color.WHITE_CODE, 104,
					196, GameConstants.TOP_LEFT_ANCHOR);

			// Global.drawBottomTaskbar(g, GameConstants.SCREEN_WIDTH, 0,
			// GameConstants.SCREEN_HEIGHT - 25);
			text8.drawString(g, "Bỏ chat", 0xffffff, 3, 213,
					GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "Gửi", 0xffffff,
					GameConstants.SCREEN_WIDTH / 2, 213,
					GameConstants.TOP_HCENTER_ANCHOR);
			text8.drawString(g, "Xóa", 0xffffff,
					GameConstants.SCREEN_WIDTH - 5, 213,
					GameConstants.TOP_RIGHT_ANCHOR);
		}
	}

	private void drawChat(Graphics g) {
		if (messageChats == null) {
			return;
		}

		for (int i = 0; i < messageChats.length; i++) {
			if (messageChats[i].equals("")) {
				continue;
			}

			int index = getDrawIndex(i);
			if (numberOfPlayer == 2) {
				if (index == 1) {
					index = 2;
				}
			} else if (numberOfPlayer == 3) {
				if (index > 0) {
					index++;
				}
			}

			switch (index) {
			case 1:
				// g.drawImage(groundChatImage, 140, 204,
				// GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, messageChats[i], Color.WHITE_CODE, 146,
						208, GameConstants.TOP_LEFT_ANCHOR);
				break;
			case 2:
				// g.drawImage(groundChatImage, 100, 10,
				// GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, messageChats[i], Color.WHITE_CODE, 106, 14,
						GameConstants.TOP_LEFT_ANCHOR);
				break;
			case 3:
				// g.drawImage(groundChatImage, 5, 75,
				// GameConstants.TOP_LEFT_ANCHOR);
				text8.drawString(g, messageChats[i], Color.WHITE_CODE, 11, 79,
						GameConstants.TOP_LEFT_ANCHOR);
				break;
			}
		}

		if (isInChatState) {
			// g
			// .drawImage(groundChatImage, 45, 270,
			// GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, messageChats[myIndex], Color.WHITE_CODE, 51,
					275, GameConstants.TOP_LEFT_ANCHOR);

			// //Global.drawBottomTaskbar(g, GameConstants.SCREEN_WIDTH, 0,
			// GameConstants.SCREEN_HEIGHT - 25);
			text8.drawString(g, "Bỏ chat", 0xffffff, 3,
					GameConstants.SCREEN_HEIGHT - 4,
					GameConstants.BOTTOM_LEFT_ANCHOR);
			text8.drawString(g, "Gửi", 0xffffff,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 4,
					GameConstants.BOTTOM_HCENTER_ANCHOR);
			text8.drawString(g, "Xóa", 0xffffff,
					GameConstants.SCREEN_WIDTH - 10,
					GameConstants.SCREEN_HEIGHT - 4,
					GameConstants.BOTTOM_RIGHT_ANCHOR);
		}
	}

	private void drawPhom320x240(Graphics g) {
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1);
			int size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2, 15,
						GameConstants.TOP_HCENTER_ANCHOR);
			}
			for (int i = size - 1; i >= 0; i--) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 184 - j * CARD_DISTANCE,
							80 - (size - i) * 18,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}
		} else if (numberOfPlayer == 3) {
			int index = getRealIndex(1);
			int size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2, 15,
						GameConstants.TOP_HCENTER_ANCHOR);
			}
			for (int i = size - 1; i >= 0; i--) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 184 - j * CARD_DISTANCE,
							80 - (size - i) * 18,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}

			index = getRealIndex(2);
			size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], 15,
						GameConstants.SCREEN_HEIGHT / 2,
						GameConstants.VCENTER_LEFT_ANCHOR);
			}
			for (int i = 0; i < size; i++) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 30 + CARD_DISTANCE * j,
							70 + 20 * i, GameConstants.TOP_LEFT_ANCHOR, false);
				}
			}
		} else if (numberOfPlayer == 4) {
			int index = getRealIndex(1);
			int size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH - 15,
						GameConstants.SCREEN_HEIGHT / 2,
						GameConstants.VCENTER_RIGHT_ANCHOR);
			}
			for (int i = 0; i < size; i++) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 234 + CARD_DISTANCE * j,
							70 + 18 * (size - i),
							GameConstants.TOP_LEFT_ANCHOR, false);
				}
			}

			index = getRealIndex(2);
			size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2, 15,
						GameConstants.TOP_HCENTER_ANCHOR);
			}
			for (int i = size - 1; i >= 0; i--) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 184 - j * CARD_DISTANCE,
							80 - (size - i) * 20,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}

			index = getRealIndex(3);
			size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], 15,
						GameConstants.SCREEN_HEIGHT / 2,
						GameConstants.VCENTER_LEFT_ANCHOR);
			}
			for (int i = 0; i < phoms[index].size(); i++) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 30 + CARD_DISTANCE * j,
							70 + 20 * i, GameConstants.TOP_LEFT_ANCHOR, true);
				}
			}
		}

		int size = phoms[myIndex].size();
		if (size == 0 && isHaphom[myIndex] && (timeDisplayMom[myIndex] < 50)) {
			g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 45,
					GameConstants.BOTTOM_HCENTER_ANCHOR);
		}
		for (int i = 0; i < size; i++) {
			PhomInfo phom = (PhomInfo) phoms[myIndex].elementAt(i);
			for (int j = 0; j < phom.size(); j++) {
				drawCardImage(g, phom.getCard(j), 118 + j * CARD_DISTANCE,
						150 + 20 * i, GameConstants.TOP_LEFT_ANCHOR, false);
			}
		}
	}

	private void drawPhom(Graphics g) { // 95: người phía bắc; 152: phía đông;
		// 30: phía tây
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1); // lấy index của người còn lại
			int size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2, 15,
						GameConstants.TOP_HCENTER_ANCHOR);
			}
			// Phỏm thứ nhất
			if (size > 2) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(2);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 123 - j * CARD_DISTANCE,
							70 /* + (phoms[index].size() - 2) * 20 */,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}
			if (size > 1) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(1);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 123 - j * CARD_DISTANCE,
							50 /* + (phoms[index].size() - 1) * 20 */,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}
			if (size > 0) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(0);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 123 - j * CARD_DISTANCE,
							30 /* + (phoms[index].size() - 0) * 20 */,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}
			// }
		} else if (numberOfPlayer == 3) {// có 3 người chơi
			int index = getRealIndex(1);// người thứ 2
			int size = phoms[index].size();
			// Móm
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2, 15,
						GameConstants.TOP_HCENTER_ANCHOR);
			}
			// Phỏm thứ nhất
			// for (int i = 0; i < phoms[index].size(); i++) {
			if (size > 2) {
				PhomInfo phom0 = (PhomInfo) phoms[index].elementAt(2);
				for (int j = 0; j < phom0.size(); j++) {
					drawCardImage(g, phom0.getCard(j), 123 - j * CARD_DISTANCE,
							70, GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}
			if (size > 1) {
				PhomInfo phom0 = (PhomInfo) phoms[index].elementAt(1);
				for (int j = 0; j < phom0.size(); j++) {
					drawCardImage(g, phom0.getCard(j), 123 - j * CARD_DISTANCE,
							50, GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}
			if (size > 0) {
				PhomInfo phom0 = (PhomInfo) phoms[index].elementAt(0);
				for (int j = 0; j < phom0.size(); j++) {
					drawCardImage(g, phom0.getCard(j), 123 - j * CARD_DISTANCE,
							30, GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}

			index = getRealIndex(2); // người thứ 3
			size = phoms[index].size();

			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], 15,
						GameConstants.SCREEN_HEIGHT / 2,
						GameConstants.VCENTER_LEFT_ANCHOR);
			}
			for (int i = 0; i < size; i++) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 30 + CARD_DISTANCE * j,
							116 + 20 * i, GameConstants.TOP_LEFT_ANCHOR, true);
				}
			}
		} else if (numberOfPlayer == 4) {

			int index = getRealIndex(1);
			int size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH - 15,
						GameConstants.SCREEN_HEIGHT / 2,
						GameConstants.VCENTER_RIGHT_ANCHOR);
			}

			for (int i = size - 1; i >= 0; i--) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 152 + CARD_DISTANCE * j,
							116 + 18 * (size - i),
							GameConstants.TOP_LEFT_ANCHOR, false);
				}
			}

			index = getRealIndex(2);
			size = phoms[index].size();

			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2, 15,
						GameConstants.TOP_HCENTER_ANCHOR);
			}
			for (int i = size - 1; i >= 0; i--) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 140 - j * CARD_DISTANCE,
							84 - (phoms[index].size() - i) * 20,
							GameConstants.BOTTOM_LEFT_ANCHOR, true);
				}
			}

			index = getRealIndex(3);
			size = phoms[index].size();
			if (size == 0 && isHaphom[index] && (timeDisplayMom[index] < 50)) {
				g.drawImage(ketQuaImage[0], 15,
						GameConstants.SCREEN_HEIGHT / 2,
						GameConstants.VCENTER_LEFT_ANCHOR);
			}
			for (int i = 0; i < phoms[index].size(); i++) {
				PhomInfo phom = (PhomInfo) phoms[index].elementAt(i);
				for (int j = 0; j < phom.size(); j++) {
					drawCardImage(g, phom.getCard(j), 30 + CARD_DISTANCE * j,
							116 + 20 * i, GameConstants.TOP_LEFT_ANCHOR, false);
				}
			}
		}
		int size = phoms[myIndex].size();
		if (size == 0 && isHaphom[myIndex] && (timeDisplayMom[myIndex] < 50)) {
			g.drawImage(ketQuaImage[0], GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 45,
					GameConstants.BOTTOM_LEFT_ANCHOR);
		}
		for (int i = 0; i < size; i++) {
			PhomInfo phom = (PhomInfo) phoms[myIndex].elementAt(i);
			for (int j = 0; j < phom.size(); j++) {
				drawCardImage(g, phom.getCard(j), 95 + j * CARD_DISTANCE,
						206 + 20 * i, GameConstants.TOP_LEFT_ANCHOR, false);
			}
		}
	}

	private void drawUserCards(Graphics g, int[][] timeLocation) {
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1);
			if (!isTimeOut[index]) {
				if (isShowUserCard[index] && !isSomeOneU) { // kết quả cuối ván,
					// các quân bài còn
					// lại
					for (int i = 0; i < userCards[index].size(); i++) {
						int cardValue = getInt(userCards[index], i);
						int cardX = GameConstants.SCREEN_WIDTH / 2
								- (userCards[index].size() * 10 + 20) / 2
								+ (userCards[index].size() - i) * CARD_DISTANCE
								- 12;
						drawCardImage(g, cardValue, cardX, 15,
								GameConstants.BOTTOM_LEFT_ANCHOR, true);
					}
				} else {
					for (int i = 0; i < userCards[index].size(); i++) {
						g.drawImage(baiUpImage, GameConstants.SCREEN_WIDTH / 2
								- (userCards[index].size() * 10 + 20) / 2 + i
								* CARD_DISTANCE - 12, 15,
								GameConstants.BOTTOM_LEFT_ANCHOR);
					}
					if (!isHaphom[index]) {
						for (int i = 0; i < gotCards[index].size(); i++) {// các
							// quân
							// ăn
							int cardValue = getInt(gotCards[index], i);
							int cardX = GameConstants.SCREEN_WIDTH
									/ 2
									- (userCards[index].size() * 10 + 20)
									/ 2
									+ (userCards[index].size() - i
											+ gotCards[index].size() - 1)
									* CARD_DISTANCE - 12;
							drawCardImage(g, cardValue, cardX, 30,
									GameConstants.BOTTOM_LEFT_ANCHOR, true);
						}
					}
				}
			}

		} else if (numberOfPlayer == 3) {
			int index = getRealIndex(1);

			if (!isTimeOut[index]) {
				if (isShowUserCard[index] && !isSomeOneU) { // 
					for (int i = 0; i < userCards[index].size(); i++) {
						int cardValue = getInt(userCards[index], i);
						int cardX = GameConstants.SCREEN_WIDTH / 2
								- (userCards[index].size() * 10 + 20) / 2
								+ (userCards[index].size() - i) * CARD_DISTANCE
								- 12;
						drawCardImage(g, cardValue, cardX, 15,
								GameConstants.BOTTOM_LEFT_ANCHOR, true);
					}
				} else {
					for (int i = 0; i < userCards[index].size(); i++) {
						g.drawImage(baiUpImage, GameConstants.SCREEN_WIDTH / 2
								- (userCards[index].size() * 10 + 20) / 2 + i
								* CARD_DISTANCE - 12, 15,
								GameConstants.BOTTOM_LEFT_ANCHOR);
					}
					if (!isHaphom[index]) {
						for (int i = 0; i < gotCards[index].size(); i++) {
							int cardValue = getInt(gotCards[index], i);
							int cardX = GameConstants.SCREEN_WIDTH
									/ 2
									- (userCards[index].size() * 10 + 20)
									/ 2
									+ (userCards[index].size() - i
											+ gotCards[index].size() - 1)
									* CARD_DISTANCE - 12;
							drawCardImage(g, cardValue, cardX, 30,
									GameConstants.BOTTOM_LEFT_ANCHOR, true);
						}
					}
				}
			}

			index = getRealIndex(2);
			if (!isTimeOut[index]) {
				if (isShowUserCard[index] && !isSomeOneU) {
					for (int i = 0; i < userCards[index].size(); i++) {
						int cardValue = getInt(userCards[index], i);
						int cardY = GameConstants.SCREEN_HEIGHT / 2
								- (userCards[index].size() * 10 + 35) / 2
								+ (userCards[index].size() - i) * CARD_DISTANCE
								- 20;
						drawCardImage(g, cardValue, 15, cardY,
								GameConstants.TOP_RIGHT_ANCHOR, false);
					}
				} else {
					if (GameConstants.IS_240x320_SCREEN) {
						for (int i = 0; i < userCards[index].size(); i++) {
							g
									.drawImage(
											baiUpImage,
											15,
											GameConstants.SCREEN_HEIGHT
													/ 2
													- (userCards[index].size() * 10 + 35)
													/ 2 + i * CARD_DISTANCE
													- 20,
											GameConstants.TOP_RIGHT_ANCHOR);
						}
					} else {
						for (int i = 0; i < userCards[index].size(); i++) {
							g
									.drawImage(
											baiUpImage,
											15,
											GameConstants.SCREEN_HEIGHT
													/ 2
													- (userCards[index].size() * 10 + 35)
													/ 2 + i * CARD_DISTANCE
													- 10,
											GameConstants.TOP_RIGHT_ANCHOR);
						}
					}
					if (!isHaphom[index]) {
						for (int i = 0; i < gotCards[index].size(); i++) {
							int cardValue = getInt(gotCards[index], i);
							int cardY = GameConstants.SCREEN_HEIGHT
									/ 2
									- (userCards[index].size() * 10 + 35)
									/ 2
									+ (userCards[index].size() - i
											+ gotCards[index].size() - 1)
									* CARD_DISTANCE - 20;
							drawCardImage(g, cardValue, 30, cardY,
									GameConstants.TOP_RIGHT_ANCHOR, false);
						}
					}
				}
			}

		} else if (numberOfPlayer == 4) {
			int index = getRealIndex(1);
			if (!isTimeOut[index]) {
				if (isShowUserCard[index] && !isSomeOneU) {
					for (int i = 0; i < userCards[index].size(); i++) {
						int cardValue = getInt(userCards[index], i);
						int cardY = GameConstants.SCREEN_HEIGHT / 2
								- (userCards[index].size() * 10 + 35) / 2 + i
								* CARD_DISTANCE - 20;
						drawCardImage(g, cardValue,
								GameConstants.SCREEN_WIDTH - 22, cardY,
								GameConstants.TOP_LEFT_ANCHOR, false);
					}
				} else {
					if (GameConstants.IS_240x320_SCREEN) {
						for (int i = 0; i < userCards[index].size(); i++) {
							g
									.drawImage(
											baiUpImage,
											GameConstants.SCREEN_WIDTH - 22,
											GameConstants.SCREEN_HEIGHT
													/ 2
													- (userCards[index].size() * 10 + 35)
													/ 2 + i * CARD_DISTANCE
													- 20,
											GameConstants.TOP_LEFT_ANCHOR);
						}
					} else {
						for (int i = 0; i < userCards[index].size(); i++) {
							g
									.drawImage(
											baiUpImage,
											GameConstants.SCREEN_WIDTH - 22,
											GameConstants.SCREEN_HEIGHT
													/ 2
													- (userCards[index].size() * 10 + 35)
													/ 2 + i * CARD_DISTANCE
													- 30,
											GameConstants.TOP_LEFT_ANCHOR);
						}
					}
					if (!isHaphom[index]) {
						for (int i = 0; i < gotCards[index].size(); i++) {
							int cardValue = getInt(gotCards[index], i);
							int cardY = GameConstants.SCREEN_HEIGHT / 2
									- (userCards[index].size() * 10 + 35) / 2
									+ (i + gotCards[index].size() - 1)
									* CARD_DISTANCE - 20;
							drawCardImage(g, cardValue,
									GameConstants.SCREEN_WIDTH - 30, cardY,
									GameConstants.TOP_LEFT_ANCHOR, false);
						}
					}
				}
			}

			index = getRealIndex(2);
			if (!isTimeOut[index]) {
				if (isShowUserCard[index] && !isSomeOneU) {
					for (int i = 0; i < userCards[index].size(); i++) {
						int cardValue = getInt(userCards[index], i);
						int cardX = GameConstants.SCREEN_WIDTH / 2
								- (userCards[index].size() * 10 + 20) / 2
								+ (userCards[index].size() - i) * CARD_DISTANCE
								- 12;
						drawCardImage(g, cardValue, cardX, 15,
								GameConstants.BOTTOM_LEFT_ANCHOR, true);
					}
				} else {
					if (!isTimeOut[index]) {
						for (int i = 0; i < userCards[index].size(); i++) {
							g.drawImage(baiUpImage, GameConstants.SCREEN_WIDTH
									/ 2 - (userCards[index].size() * 10 + 20)
									/ 2 + i * CARD_DISTANCE - 12, 15,
									GameConstants.BOTTOM_LEFT_ANCHOR);
						}
					}
					if (!isHaphom[index]) {
						for (int i = 0; i < gotCards[index].size(); i++) {
							int cardValue = getInt(gotCards[index], i);
							int cardX = GameConstants.SCREEN_WIDTH
									/ 2
									- (userCards[index].size() * 10 + 20)
									/ 2
									+ (userCards[index].size() - i
											+ gotCards[index].size() - 1)
									* CARD_DISTANCE - 12;
							drawCardImage(g, cardValue, cardX, 30,
									GameConstants.BOTTOM_LEFT_ANCHOR, true);
						}
					}
				}
			}
			index = getRealIndex(3);
			if (!isTimeOut[index]) {
				if (isShowUserCard[index] && !isSomeOneU) {
					for (int i = 0; i < userCards[index].size(); i++) {
						int cardValue = getInt(userCards[index], i);
						int cardY = GameConstants.SCREEN_HEIGHT / 2
								- (userCards[index].size() * 10 + 35) / 2
								+ (userCards[index].size() - i) * CARD_DISTANCE
								- 20;
						drawCardImage(g, cardValue, 22, cardY,
								GameConstants.TOP_RIGHT_ANCHOR, false);
					}
				} else {
					if (!isTimeOut[index]) {
						if (GameConstants.IS_240x320_SCREEN) {
							for (int i = 0; i < userCards[index].size(); i++) {
								g
										.drawImage(
												baiUpImage,
												22,
												GameConstants.SCREEN_HEIGHT
														/ 2
														- (userCards[index]
																.size() * 10 + 35)
														/ 2 + i * CARD_DISTANCE
														- 20,
												GameConstants.TOP_RIGHT_ANCHOR);
							}
						} else {
							for (int i = 0; i < userCards[index].size(); i++) {
								g
										.drawImage(
												baiUpImage,
												22,
												GameConstants.SCREEN_HEIGHT
														/ 2
														- (userCards[index]
																.size() * 10 + 35)
														/ 2 + i * CARD_DISTANCE
														- 10,
												GameConstants.TOP_RIGHT_ANCHOR);
							}
						}
					}
					if (!isHaphom[index]) {
						for (int i = 0; i < gotCards[index].size(); i++) {
							int cardValue = getInt(gotCards[index], i);
							int cardY = GameConstants.SCREEN_HEIGHT
									/ 2
									- (userCards[index].size() * 10 + 35)
									/ 2
									+ (userCards[index].size() - i
											+ gotCards[index].size() - 1)
									* CARD_DISTANCE - 20;
							drawCardImage(g, cardValue, 30, cardY,
									GameConstants.TOP_RIGHT_ANCHOR, false);
						}
					}
				}
			}
		}

		int userCardSize = userCards[myIndex].size();
		if (GameConstants.IS_240x320_SCREEN) {
			for (int i = 0; i < userCardSize; i++) {
				int cardValue = getInt(userCards[myIndex], i);
				int cardX = GameConstants.SCREEN_WIDTH / 2
						- (userCardSize * 10 + 20) / 2 + i * CARD_DISTANCE - 12;
				int cardY = GameConstants.SCREEN_HEIGHT - 49;
				if (getIndexByValue(selectedCardByUser, cardValue) > -1) {
					cardY -= 8;
					drawSelectedCardImage(g, cardValue, cardX, cardY,
							GameConstants.TOP_LEFT_ANCHOR);
				} else if (getIndexByValue(gotCards[myIndex], cardValue) > -1) { // Nếu
					// là
					// cây
					// bài
					// đã
					// ăn
					// thì
					// cho
					// thụt
					// xuống
					drawCardImage(g, cardValue, cardX, cardY,
							GameConstants.TOP_LEFT_ANCHOR, false);
					g.setColor(0x00FF00);
					g.drawRoundRect(cardX + 2, cardY + 1, 28, 45, 7, 7);
					g.drawRoundRect(cardX + 3, cardY + 2, 26, 43, 7, 7);

				} else {
					if (i == movePointer) {
						if (isCardsChange) {
							drawSelectedCardImage(g, cardValue, cardX,
									cardY + 8, GameConstants.TOP_LEFT_ANCHOR);
						} else {
							drawSelectedCardImage(g, cardValue, cardX, cardY,
									GameConstants.TOP_LEFT_ANCHOR);
						}
					} else {
						drawCardImage(g, cardValue, cardX, cardY,
								GameConstants.TOP_LEFT_ANCHOR, false);
					}
				}
			}
		} else {
			for (int i = 0; i < userCardSize; i++) {
				int cardValue = getInt(userCards[myIndex], i);
				int cardX = GameConstants.SCREEN_WIDTH / 2
						- (userCardSize * 10 + 20) / 2 + i * CARD_DISTANCE - 12;
				int cardY = GameConstants.SCREEN_HEIGHT - 49;
				if (getIndexByValue(selectedCardByUser, cardValue) > -1) {
					cardY -= 8;
					drawSelectedCardImage(g, cardValue, cardX, cardY,
							GameConstants.TOP_LEFT_ANCHOR);
				} else if (getIndexByValue(gotCards[myIndex], cardValue) > -1) { // Nếu
					// là
					// cây
					// bài
					// đã
					// ăn
					// thì
					// cho
					// thụt
					// xuống
					drawCardImage(g, cardValue, cardX, cardY,
							GameConstants.TOP_LEFT_ANCHOR, false);
					g.setColor(0x00FF00);
					g.drawRoundRect(cardX + 2, cardY + 1, 28, 45, 7, 7);
					g.drawRoundRect(cardX + 3, cardY + 2, 26, 43, 7, 7);
				} else {
					if (i == movePointer) {
						drawSelectedCardImage(g, cardValue, cardX, cardY,
								GameConstants.TOP_LEFT_ANCHOR);
					} else {
						drawCardImage(g, cardValue, cardX, cardY,
								GameConstants.TOP_LEFT_ANCHOR, false);
					}
				}
			}
		}

		// Vẽ bài còn lại trên bàn
		g.drawImage(baiUpImage, GameConstants.SCREEN_WIDTH / 2,
				GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
		text8.drawString(g, String.valueOf(remainCard), Color.WHITE_CODE,
				GameConstants.SCREEN_WIDTH / 2,
				GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);

		// Đếm thời gian của người chơi hiện tại
		if (currentTimeCount > -1) {
			int index = getDrawIndex(currentTimeIndex);
			if (numberOfPlayer == 2) {
				if (index == 1) {
					index = 2;
				}
			} else if (numberOfPlayer == 3) {
				if (index > 0) {
					index++;
				}
			}
			g.drawImage(numberBgImage, timeLocation[index][0],
					timeLocation[index][1], GameConstants.CENTER_ANCHOR);
			text8.drawString(g, String.valueOf(currentTimeCount),
					Color.WHITE_CODE, timeLocation[index][0],
					timeLocation[index][1], GameConstants.CENTER_ANCHOR);
		}

		// Draw poiter
		if (userCardSize > 0) {
			if (movePointer > userCards[myIndex].size() - 1) {
				movePointer = userCards[myIndex].size() - 1;
			}
			int card = Integer.parseInt(String.valueOf(userCards[myIndex]
					.elementAt(movePointer)));
			if (getIndexByValue(selectedCardByUser, card) == -1) {
				g.drawImage(pointerImage, GameConstants.SCREEN_WIDTH / 2
						- (userCardSize * 10 + 20) / 2 - 7 + CARD_DISTANCE
						* movePointer, GameConstants.SCREEN_HEIGHT - 52,
						GameConstants.BOTTOM_HCENTER_ANCHOR);
			} else {
				g.drawImage(pointerImage, GameConstants.SCREEN_WIDTH / 2
						- (userCardSize * 10 + 20) / 2 - 7 + CARD_DISTANCE
						* movePointer, GameConstants.SCREEN_HEIGHT - 62,
						GameConstants.BOTTOM_HCENTER_ANCHOR);
			}
		}
	}

	private void drawCommand(Graphics g) {
    // Draw bottom bar
    g.setColor(0x2B0102);
    g.fillRect(0, GameConstants.SCREEN_HEIGHT - 25, GameConstants.SCREEN_WIDTH, 25);
		
		if (state == HA_PHOM_STATE) {
			text8.drawString(g, "Hạ phỏm", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 25,
					GameConstants.TOP_HCENTER_ANCHOR);
		} else if (state == GET_CARD_STATE) {
			text8.drawString(g, "Bốc", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 25,
					GameConstants.TOP_HCENTER_ANCHOR);
			text8.drawString(g, "Ăn", Color.WHITE_CODE, 5,
					GameConstants.SCREEN_HEIGHT - 25,
					GameConstants.TOP_LEFT_ANCHOR);
		} else if (state == FIRE_CARD_STATE && !isShowingFinalResult) {
			text8.drawString(g, "Đánh", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 25,
					GameConstants.TOP_HCENTER_ANCHOR);
			if (isGuiBai) {
				text8.drawString(g, "Gửi bài", Color.WHITE_CODE, 5,
						GameConstants.SCREEN_HEIGHT - 25,
						GameConstants.TOP_LEFT_ANCHOR);
			}
			if (selectedCardByUser.size() > 0) {
			}
		} else if (state == WAIT_STATE) {
			if (selectedCardByUser.size() > 0) {
			}
		}
		g.setColor(Color.WHITE_CODE);
		if (!isShowingFinalResult) {
			if (menu.isShowing()) {
				text8.drawString(g, "Đóng", Color.WHITE_CODE,
						GameConstants.SCREEN_WIDTH - 5,
						GameConstants.SCREEN_HEIGHT - 25,
						GameConstants.TOP_RIGHT_ANCHOR);
			} else {
				text8.drawString(g, "Menu", Color.WHITE_CODE,
						GameConstants.SCREEN_WIDTH - 5,
						GameConstants.SCREEN_HEIGHT - 25,
						GameConstants.TOP_RIGHT_ANCHOR);
			}
		}

		if (isShowingFinalResult) {
			text8.drawString(g, "Chơi tiếp", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT - 25,
					GameConstants.TOP_HCENTER_ANCHOR);
			text8.drawString(g, "Thoát", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH - 5,
					GameConstants.SCREEN_HEIGHT - 25,
					GameConstants.TOP_RIGHT_ANCHOR);
		}
	}

	private void drawFiredCard320x240(Graphics g) {
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 65 + i * CARD_DISTANCE, 38,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			int x = 254 - firedCards[myIndex].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[myIndex].size(); i++) {
				int value = getInt(firedCards[myIndex], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 176,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}
		} else if (numberOfPlayer == 3) {
			int index = getRealIndex(2);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 65 + i * CARD_DISTANCE, 176,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}

			index = getRealIndex(1);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 65 + i * CARD_DISTANCE, 38,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			int x = 254 - firedCards[myIndex].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[myIndex].size(); i++) {
				int value = getInt(firedCards[myIndex], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 176,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}
		} else if (numberOfPlayer == 4) {
			int index = getRealIndex(1);
			int x = 254 - firedCards[index].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 38,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			index = getRealIndex(2);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 65 + i * CARD_DISTANCE, 38,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			index = getRealIndex(3);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 65 + i * CARD_DISTANCE, 176,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}

			x = 254 - firedCards[myIndex].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[myIndex].size(); i++) {
				int value = getInt(firedCards[myIndex], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 180,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}
		}
	}

	private void drawFiredCard(Graphics g) {
		if (numberOfPlayer == 2) {
			int index = getRealIndex(1);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 45 + i * CARD_DISTANCE, 66,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			int x = 170 - firedCards[myIndex].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[myIndex].size(); i++) {
				int value = getInt(firedCards[myIndex], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 233,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}
		} else if (numberOfPlayer == 3) {
			int index = getRealIndex(2);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 45 + i * CARD_DISTANCE, 233,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}

			index = getRealIndex(1);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 45 + i * CARD_DISTANCE, 66,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			int x = 170 - firedCards[myIndex].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[myIndex].size(); i++) {
				int value = getInt(firedCards[myIndex], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 233,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}
		} else if (numberOfPlayer == 4) {
			int index = getRealIndex(1);
			int x = 170 - firedCards[index].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 66,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			index = getRealIndex(2);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 45 + i * CARD_DISTANCE, 66,
						GameConstants.TOP_LEFT_ANCHOR, false);
			}

			index = getRealIndex(3);
			for (int i = 0; i < firedCards[index].size(); i++) {
				int value = getInt(firedCards[index], i);
				drawCardImage(g, value, 45 + i * CARD_DISTANCE, 233,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}

			x = 170 - firedCards[myIndex].size() * CARD_DISTANCE;
			for (int i = 0; i < firedCards[myIndex].size(); i++) {
				int value = getInt(firedCards[myIndex], i);
				drawCardImage(g, value, x + i * CARD_DISTANCE, 233,
						GameConstants.BOTTOM_LEFT_ANCHOR, false);
			}
		}
	}

	private void drawMessage(Graphics g) {
		g.setColor(0xFFA500);
		if (messageIndex > -1) {
			g.fillRoundRect(GameConstants.SCREEN_WIDTH / 2 - 80,
					GameConstants.SCREEN_HEIGHT / 2 - 22, 160, 24, 5, 5);
		}
		switch (messageIndex) {
		case 0:
			text8.drawString(g, "Bài ăn không hợp lệ", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		case 1:
			text8.drawString(g, "Bài đánh không hợp lệ", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		case 2:
			text8.drawString(g, "Hạ bài", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		case 4:
			text8.drawString(g, "Bạn phải chọn bài để ăn phỏm",
					Color.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		case 5:
			text8.drawString(g, "Bạn phải nhấc bài để đánh", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		case 6:
			text8.drawString(g, "Phỏm hạ không hợp lệ", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		case 7:
			text8.drawString(g, "Bài gửi không hợp lệ", Color.WHITE_CODE,
					GameConstants.SCREEN_WIDTH / 2,
					GameConstants.SCREEN_HEIGHT / 2 - 20,
					GameConstants.TOP_HCENTER_ANCHOR);
			break;
		}
	}

	private void drawCardImage(Graphics g, int quanBai, int x, int y,
			int anchor, boolean isRotate) {
		if (!isRotate) {
			drawCardImage(g, cardImges[quanBai / 13], quanBai, x, y, anchor);
		} else {
			drawCardImage(g, Image.createImage(cardImges[quanBai / 13], 0, 0,
					cardImges[quanBai / 13].getWidth(), cardImges[quanBai / 13]
							.getHeight(), Sprite.TRANS_ROT180), quanBai, x, y,
					anchor);
		}
	}

	private void drawSelectedCardImage(Graphics g, int quanBai, int x, int y,
			int anchor) {
		drawCardImage(g, selectedCardImges[quanBai / 13], quanBai, x, y, anchor);
	}

	private void drawCardImage(Graphics g, Image cardImage, int quanBai, int x,
			int y, int anchor) {
		g.drawImage(cardImage, x, y, anchor);

		int width = cardImage.getWidth();
		int realX = x;
		switch (anchor) {
		case GameConstants.TOP_HCENTER_ANCHOR:
		case GameConstants.CENTER_ANCHOR:
		case GameConstants.BOTTOM_HCENTER_ANCHOR:
			realX = x - width / 2;
			break;
		case GameConstants.TOP_RIGHT_ANCHOR:
		case GameConstants.VCENTER_RIGHT_ANCHOR:
		case GameConstants.BOTTOM_RIGHT_ANCHOR:
			realX = x - width;
			break;
		}

		int height = cardImage.getHeight();
		int realY = y;
		switch (anchor) {
		case GameConstants.VCENTER_LEFT_ANCHOR:
		case GameConstants.CENTER_ANCHOR:
		case GameConstants.VCENTER_RIGHT_ANCHOR:
			realY = y - height / 2;
			break;
		case GameConstants.BOTTOM_LEFT_ANCHOR:
		case GameConstants.BOTTOM_HCENTER_ANCHOR:
		case GameConstants.BOTTOM_RIGHT_ANCHOR:
			realY = y - height;
			break;
		}

		int color = Color.RED_CODE;
		if ((quanBai / 13 == 0) || (quanBai / 13 == 1)) {
			color = Color.BLACK_CODE;
		}
		int mod = quanBai % 13;
		if (mod == 0) {
			text8.drawString(g, "A", color, realX + 4, realY,
					GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "A", color, realX + width - 2, realY + height
					- 2, GameConstants.BOTTOM_RIGHT_ANCHOR);
		} else if ((0 < mod) && (mod < 10)) {
			text8.drawString(g, String.valueOf(mod + 1), color, realX + 4,
					realY, GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, String.valueOf(mod + 1), color, realX + width
					- 2, realY + height - 2, GameConstants.BOTTOM_RIGHT_ANCHOR);
		} else if (mod == 10) {
			text8.drawString(g, "J", color, realX + 4, realY,
					GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "J", color, realX + width - 2, realY + height
					- 2, GameConstants.BOTTOM_RIGHT_ANCHOR);
		} else if (mod == 11) {
			text8.drawString(g, "Q", color, realX + 4, realY,
					GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "Q", color, realX + width - 2, realY + height
					- 2, GameConstants.BOTTOM_RIGHT_ANCHOR);
		} else if (mod == 12) {
			text8.drawString(g, "K", color, realX + 4, realY,
					GameConstants.TOP_LEFT_ANCHOR);
			text8.drawString(g, "K", color, realX + width - 2, realY + height
					- 2, GameConstants.BOTTOM_RIGHT_ANCHOR);
		}
	}

	private void sapXepTangDanTheoCaNgang(Vector cards) {
		if (cards == null) {
			return;
		}
		for (int i = 0; i < cards.size() - 1; i++) {
			for (int j = i + 1; j < cards.size(); j++) {
				Object info = cards.elementAt(i);
				Object infoTemp = cards.elementAt(j);
				if (Integer.parseInt(info.toString()) % 13 > Integer
						.parseInt(infoTemp.toString()) % 13) {
					cards.setElementAt(infoTemp, i);
					cards.setElementAt(info, j);
				}
			}
		}
	}

	public void alertEventPerform(int alertType, int eventType, int alertId) {
		if (alertId == 99) { // Thoát và quay trở về room
			GameGlobal.nextState(Global.frmTable, null,
					Transformer.TRANSFORM_WITH_LOADING_FORM);
		} else if (alertId == 98) {
			if (eventType == Alert.YES_BUTTON) {
				state = FIRE_CARD_STATE;
				currentTurn++;
			}
		} else if (alertId == 97) {
			if (eventType == Alert.YES_BUTTON) {
				SocketClientUtil.leaveTableRequest();
				GameGlobal.nextState(Global.frmListTable, null,
						Transformer.TRANSFORM_WITH_LOADING_FORM);
			}
		}
	}

	private static boolean isCaDoc(Vector quanBais) {
		quanBais = VerticalSort(quanBais);
		int quanBaiLen = quanBais.size();
		if (quanBaiLen <= 2) {
			return false;
		}
		for (int i = 0; i < quanBaiLen - 1; i++) {
			if (Math.abs(Integer.parseInt(quanBais.elementAt(i + 1).toString())
					- Integer.parseInt(quanBais.elementAt(i).toString())) != 1) {
				return false;
			}
		}
		if ((Integer.parseInt(quanBais.firstElement().toString()) / 13 != Integer
				.parseInt(quanBais.lastElement().toString()) / 13)) {
			return false;
		}
		return true;
	}

	private static boolean isCaNgang(Vector quanBais, int quanBai) {
		quanBais.addElement(new Integer(quanBai));
		int quanBaiLen = quanBais.size();
		if (quanBaiLen <= 2) {
			return false;
		}
		for (int i = 0; i < quanBaiLen - 1; i++) {
			if (Integer.parseInt(quanBais.elementAt(i).toString()) % 13 != (Integer
					.parseInt(quanBais.elementAt(i + 1).toString()) % 13)) {
				return false;
			}
		}
		return true;
	}

	private static Vector VerticalSort(Vector cards) {
		if (cards == null) {
			return null;
		}
		int size = cards.size();
		for (int i = 0; i < size - 1; i++) {
			for (int j = i + 1; j < size; j++) {
				int info = Integer.parseInt(cards.elementAt(i).toString());
				int infoTemp = Integer.parseInt(cards.elementAt(j).toString());
				if (info > infoTemp) {
					cards.setElementAt(new Integer(infoTemp), i);
					cards.setElementAt(new Integer(info), j);
				}
			}
		}
		return cards;
	}

	protected void destroy() {
		selectedCardImges = null;
		avatarBgImages = null;
		avatarDefautl = null;
		numberBgImage = null;
		userJidU = null;
		currentText = null;
		baiUpImage = null;
		pointerImage = null;
		cardImges = null;
		if (userCards != null) {
			for (int i = 0; i < userCards.length; i++) {
				userCards[i].removeAllElements();
				userCards[i] = null;
			}
		}
		userCards = null;

		if (firedCards != null) {
			for (int i = 0; i < firedCards.length; i++) {
				firedCards[i].removeAllElements();
				firedCards[i] = null;
			}
		}
		firedCards = null;
		if (gotCards != null) {
			for (int i = 0; i < gotCards.length; i++) {
				gotCards[i].removeAllElements();
				gotCards[i] = null;
			}
		}
		gotCards = null;
		if (phoms != null) {
			for (int i = 0; i < phoms.length; i++) {
				phoms[i].removeAllElements();
				phoms[i] = null;
			}
		}
		phoms = null;
		if (selectedCardByUser != null) {
			selectedCardByUser.removeAllElements();
			selectedCardByUser = null;
		}

		leftImage = null;
		rightImage = null;
		bodyFrameImage = null;
		ketQuaImage = null;
		nhatImage = null;
		uImage = null;
		resultName = null;
		leftPoint = null;
		resultY = null;
		resultLabel = null;
		isShowUserCard = null;
		if (belongToPhoms != null) {
			belongToPhoms.removeAllElements();
			belongToPhoms = null;
		}
		cardPoint = null;
		// groundChatImage = null;
		rank = null;
		messageDelayCount = null;
		messageChats = null;
		if (menu != null) {
			menu.detroy();
			menu = null;
		}
		MENU = null;
		tableUsers = null;
	}

	public void onActionPerform(Event event) {
		// TODO Auto-generated method stub
		String action = event.getAction();
		if (action.equals(MENU[1])) { // Sắp xếp
			sapXepTangDanTheoCaNgang(userCards[myIndex]);
		} else if (action.equals(MENU[2])) { // Thoát
			GameGlobal.alert.showAlert(this, Alert.YES_NO_TYPE, new String[] {
					"Thoát ra sẽ bị tính là thua cuộc", "Bạn có muốn thoát?" },
					97);
		}

	}

	public void onConnectDone() {
		// TODO Auto-generated method stub

	}

	public void onConnectFail() {
		// TODO Auto-generated method stub

	}

	public void onDisconnect() {
		// TODO Auto-generated method stub

	}
}
